<?php
class AdminDreamBook extends CActiveRecord{

    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'dream_book';
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){}

    public function getRowsBySomo(){        
        $row = array();
        $sql = "SELECT `id`,`title`,`result` FROM `dream_book`";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();
        return $row;
    }

    public function getRowsByTitleSomo($title){

        $row = array();        
        $sql = "SELECT `id`,`title`,`result` FROM `dream_book` WHERE `title` LIKE '%".$title."%'";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);            
        $row = $command->queryAll();                        
        return $row;
    }

    public function getRowsById($id){

        $row = array();        
        $sql = "SELECT `id`,`title`,`result` FROM `dream_book` WHERE `id` = '".$id."'";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);            
        $row = $command->queryAll();                        
        return $row;
    }


    public function getRowsBySomoPaging($begin,$end){        
        $row = array();        
        $sql = "SELECT `id`,`title`,`result` FROM `dream_book` LIMIT ".$begin.",".$end;            
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();            
        return $row;
    }

    public function getRowsByTitleSomoPaging($title,$begin,$end){        
        $row = array();        
        $sql = "SELECT `id`,`title`,`result` FROM `dream_book` WHERE `title` LIKE '%".$title."%'  LIMIT ".$begin.",".$end;            
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);            
        $row = $command->queryAll();                        
        return $row;
    }

    public function deleteRowById($id)
    {
        $row = array();                        
        $sql = "DELETE FROM `dream_book` WHERE `dream_book`.`id` = ".$id." ";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->execute();            
        return $row;
    }

    public function updateRowById($id,$ten,$so)
    {
        $date = date('Y-m-d',time());
        $row = array();                        
        $sql = "UPDATE `dream_book` SET `title` = '".$ten."',`modify_date` = '".$date."', `result` = '".$so."' WHERE `dream_book`.`id` = ".$id." ";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->execute();            
        return $row;
    }
    
    public function insertRow($ten,$so)
    {
        $date = date('Y-m-d',time());
        $row = array();                        
        $sql = "INSERT INTO `dream_book` (`title`, `result`, `create_date`) VALUES ('".$ten."','".$so."','".$date."');";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->execute();            
        return $row;
    }
}
